package pl.edu.agh.ki.ewywiadowka.client;

import java.util.LinkedList;

import pl.edu.agh.ki.ewywiadowka.client.dto.AttendantDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.MarkDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.SystemUserDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.TeacherDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;
import pl.edu.agh.ki.ewywiadowka.client.windows.LoginWindow;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class EWywiadowka implements EntryPoint {

	/**
	 * UI elements
	 */
	private DockPanel _mainPanel;
	private Widget _actualWindow;
	private Widget _actualMenu;
	private Label _currentlyLoggedLabel;
	private Button _logoutButton;

	/**
	 * User attributes
	 */
	private static final String[] __attendantLabels = {
			Const.MenuButtonNames.MARKS_SINGLE,
			Const.MenuButtonNames.MARKS_FINAL,
			Const.MenuButtonNames.ABSENCE 
			};

	private static final String[] __initialLabels = {
		
			};

	private static final String[] __secretaryLabels = {
			Const.MenuButtonNames.ADD_CLASS,
			Const.MenuButtonNames.MODIFY_CLASS,
			Const.MenuButtonNames.REMOVE_CLASS,
			Const.MenuButtonNames.ADD_STUDENT_ATTENDANT,
			Const.MenuButtonNames.MODIFY_STUDENT,
			Const.MenuButtonNames.REMOVE_STUDENT,
			Const.MenuButtonNames.MODIFY_ATTENDANT,
			Const.MenuButtonNames.REMOVE_ATTENDANT,
			Const.MenuButtonNames.ADD_TEACHER,
			Const.MenuButtonNames.MODIFY_TEACHER,
			Const.MenuButtonNames.REMOVE_TEACHER
			};

	private static final String[] __educatorLabels = {
			Const.MenuButtonNames.MARKS_SINGLE,
			Const.MenuButtonNames.MARKS_FINAL,
			Const.MenuButtonNames.ABSENCE
			};

	private static final String[] __teacherLabels = {

			};

	MenuClickHandler _menuClickHandler = new MenuClickHandler(this);

	/**
	 * Data containers
	 */
	private LinkedList<MarkDTO> _marks;
	private String _currentlyLogged;
	private Const.Attributes _currentAttributes;
	private Label _currentAttributesLabel;

	/**
	 * Kiedy jest zalogowany wychowawca
	 */
	public ClassDTO _clas;
	public TeacherDTO _educator;
	
	/**
	 * Kiedy jest zalogowany opiekun
	 */
	public AttendantDTO _attendant;
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		_mainPanel = new DockPanel();

		_mainPanel.setSpacing(4);
		_mainPanel.setBorderWidth(2);
		_mainPanel.setPixelSize((int) (Window.getClientWidth() * 0.98),
				(int) (Window.getClientHeight() * 0.98));
		_mainPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);

		Window.addResizeHandler(new ResizeHandler() {
			@Override
			public void onResize(ResizeEvent event) {
				_mainPanel.setPixelSize((int) (event.getWidth() * 0.98),
						(int) (event.getHeight() * 0.98));
			}
		});

		// Naglowek - moze byc grafika, cos w ten desen
		_mainPanel.add(new HTML("<h1>e-Wywiadowka</h1>"), DockPanel.NORTH);

		// Stopka
		_mainPanel
				.add(new HTML("Copyright by AGH, 1919-2009"), DockPanel.SOUTH);

		// Prawa strona, hmm, moze zawierac jakies informacje diagnostyczne?
		// helpa dla kazdej ze stron
		//_mainPanel.add(new HTML(
		//		"Miejsce na Twoja reklame :)<br>Tutaj moze byc jakis help"),
		//		DockPanel.EAST);

		// Menu
		VerticalPanel mainMenu = new VerticalPanel();
		_actualMenu = mainMenu;
		for (String label : __initialLabels) {
			Button button = new Button(label);
			button.addClickHandler(_menuClickHandler);
			mainMenu.add(button);
		}
		_mainPanel.add(mainMenu, DockPanel.WEST);

		// Belka systemowa
		//_mainPanel.add(new HTML(
		//		"Belka systemowa, np podpowiedzi przy najechaniu na buttony"),
		//		DockPanel.SOUTH);

		// defaultowo mamy widget do logowania
		_actualWindow = new LoginWindow(this);
		_mainPanel.add(_actualWindow, DockPanel.CENTER);
		_mainPanel.setCellHeight(_actualWindow, "100%");
		_mainPanel.setCellWidth(_actualWindow, "100%");

		// Belka informacyjna, szybkie logowanie?
		HorizontalPanel informationBar = new HorizontalPanel();
		informationBar.add(new Label("Jestes zalogowany jako:\t"));

		// w zalenosci od uprawnienen, nazwy uzytkownika wyswietlamy odpowiednio
		_currentlyLoggedLabel = new Label("");
		_currentAttributesLabel = new Label(Const.Attributes.UNKNOWN.toString());
		_logoutButton = new Button(Const.MenuButtonNames.LOG_OUT);

		_logoutButton.addClickHandler(_menuClickHandler);
		
		informationBar.add(_currentlyLoggedLabel);
		informationBar.add(new Label("\t   ||   uprawnienia: "));
		informationBar.add(new Label("  "));
		informationBar.add(_currentAttributesLabel);
		informationBar.add(_logoutButton);

		_mainPanel.add(informationBar, DockPanel.NORTH);

		RootPanel.get("mainPanel").add(_mainPanel);

	}

	public LinkedList<MarkDTO> getMarks() {
		return _marks;
	}

	public void putWindow(GenericWindow window) {
		_mainPanel.remove(_actualWindow);
		_mainPanel.add(window, DockPanel.CENTER);
		_actualWindow = window;
		_mainPanel.setCellHeight(_actualWindow, "100%");
		_mainPanel.setCellWidth(_actualWindow, "100%");
	}

	public void setUser(SystemUserDTO user) {
		_currentlyLogged = user.user.login;
		_currentAttributes = user.user.attr;

		// tutaj pobranie uprawnien dla loginu i na ich podstawie wstawienie
		// odpowiednich buttonow w menu

		_mainPanel.remove(_actualMenu);
		VerticalPanel mainMenu = new VerticalPanel();
		_actualMenu = mainMenu;

		String[] labels = null;

		if (_currentAttributes != null) {
			
			_currentAttributesLabel.setText(_currentAttributes.toString());
			_currentlyLoggedLabel.setText(_currentlyLogged);
			
			switch (_currentAttributes) {
			case STUDENT:
				labels = __attendantLabels;
				break;
			case SECRETARY:
				labels = __secretaryLabels;
				break;
			case ATTENDANT:
				labels = __attendantLabels;
				break;
			case EDUCATOR:
				labels = __educatorLabels;
				break;
			case TEACHER:
				labels = __teacherLabels;
				break;
			}
			
			for (String label : labels) {
				Button button = new Button(label);
				button.setWidth("200px");
				button.addClickHandler(_menuClickHandler);
				mainMenu.add(button);
			}
			
		} else {
			_currentAttributesLabel.setText(Const.Attributes.UNKNOWN.toString());
			_currentlyLoggedLabel.setText("");
		}
		_mainPanel.add(mainMenu, DockPanel.WEST);
	}

	public String getLogin() {
		return _currentlyLogged;
	}

	public void setMarks(LinkedList<MarkDTO> result) {
		_marks = result;
	}

	public Const.Attributes getAttributes() {
		return _currentAttributes;
	}

}
